import {
  // 反走私情报数据概览
  querySummaryCount,
  querySmuSummaryCount,
  queryGoodsType,
  queryDataSource,
  queryAreaWarn,
  queryKeyword,
  queryDynamicTypeRatio,
  queryGoodsTop,
  querySmuWarnType,
  queryWarnTop,
  querySourceUrl,
  queryAreaNums
} from '@/views/daping/api/index.js'

// 统一配置成{name: xxx, value: xxx}格式
function setChartPieData(list, nameKey, valueKey, radioKey) {
  let data = []
  data = list.map(item => {
    return {
      name: item[nameKey],
      value: item[valueKey] || 0,
      ratio: item[radioKey] ? item[radioKey] + '%' : 0
    }
  })
  return data
}

function setChartBarData(list, nameKey, valueKey) {
  let data = []
  data = list.map(item => {
    return {
      product: item[nameKey],
      count: item[valueKey] || 0
    }
  })
  return data
}

function setGroupPieData(list, nameKey, valueKey, childrenKey) {
  let data = []

  list.map(item => {
    let obj = {
      name: item[nameKey],
      value: item[valueKey] || 0,
      children: []
    }
    item[childrenKey].map(child => {
      let value = (child[valueKey] / item[valueKey]) * 100
      if (value % 1 !== 0) {
        value = Number(value.toFixed(2))
      }
      obj.children.push({
        name: child[nameKey],
        value: value
      })
    })
    data.push(obj)
  })

  return data
}

function setChartLineData(list, childKey, labelKey, nameKey, valueKey) {
  let obj = {}
  list.map(item => {
    item[childKey].map(child => {
      obj[child[labelKey]] = []
    })
  })
  list.map(item => {
    let itemDate = item[nameKey]
    item[childKey].map(child => {
      obj[child[labelKey]].push({
        name: itemDate || child[nameKey],
        value: child[valueKey] || 0
      })
    })
  })
  return obj
}

/**
 * 自定义函数名：arrSort
 * @param arr： 被操作数
 * @param key： 需要用作排序的key名
 */
function arrSort(arr, key) {
  arr.sort(function(a, b) {
    return b[key] - a[key]
  })
  return arr
}

// 反走私情报数据概览
async function _querySummaryCount(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await querySummaryCount(query)
  let data = []
  for (let key in result) {
    let obj1 = result[key]
    let obj2 = { key: key }
    let obj = Object.assign({}, obj1, obj2)
    data.push(obj)
  }
  return data
}

// 告警类型概况
async function _querySmuSummaryCount(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await querySmuSummaryCount(query)
  let data = []
  for (let key in result) {
    let obj1 = result[key]
    let obj2 = { key: key }
    let obj = Object.assign({}, obj1, obj2)
    data.push(obj)
  }
  return data
}

// 情报商品类型变化趋势
async function _queryGoodsType(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await queryGoodsType(query)
  return setChartLineData(result, 'valueList', 'label', 'date', 'value')
}

// 数据来源情报推送占比
async function _queryDataSource(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await queryDataSource(query)
  return setChartPieData(result, 'label', 'value', 'radio')
}

// 告警区域数量排行
async function _queryAreaWarn(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await queryAreaWarn(query)
  return setChartBarData(result, 'label', 'value')
}

// 走私情报关键词排行
async function _queryKeyword(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await queryKeyword(query)
  return setChartBarData(result, 'label', 'value')
}

// 数据来源告警动态占比
async function _queryDynamicTypeRatio(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate,
    category: 1 // 1，数据来源；2，告警类型
  }
  const { result } = await queryDynamicTypeRatio(query)
  return setChartPieData(result, 'label', 'value')
}

// 告警类型变化趋势
async function _querySmuWarnType(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await querySmuWarnType(query)
  return setChartLineData(result, 'valueList', 'label', 'date', 'value')
}

// 走私告警账户排行
async function _queryWarnTop(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate,
    category: 2 // 1，网站告警；2，账户告警；3，告警关键词
  }
  const res = await queryWarnTop(query)
  let data = []
  if (res.result && res.result.length) {
    let result = arrSort(res.result, 'value') //排序
    data = result
      .map((item, index) => {
        if (index < 5) {
          //只取前五的数组值
          return {
            product: item.label,
            count: item.value
          }
        }
      })
      .filter(obj => obj !== undefined)
  }
  return data
}

// 走私贩卖网站排行
async function _querySourceUrl(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const res = await querySourceUrl(query)
  let data = []
  if (res.result && res.result.length) {
    let result = arrSort(res.result, 'value') //排序
    data = result
      .map((item, index) => {
        if (index < 5) {
          //只取前五的数组值
          return {
            product: item.label,
            count: item.value
          }
        }
      })
      .filter(obj => obj !== undefined)
  }
  return data
}

// 走私贩卖物品排行
async function _queryGoodsTop(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await queryGoodsTop(query)
  return setChartBarData(result, 'label', 'value')
}

// 特定区域数量变化趋势
async function _queryAreaNums(params) {
  const query = {
    timeType: params.dateType,
    startTime: params.startDate,
    endTime: params.endDate
  }
  const { result } = await queryAreaNums(query)
  return setChartLineData(result, 'valueList', 'label', 'date', 'value')
}

export default {
  _querySummaryCount,
  _querySmuSummaryCount,
  _queryGoodsType,
  _queryDataSource,
  _queryKeyword,
  _queryAreaWarn,
  _queryDynamicTypeRatio,
  _querySmuWarnType,
  _querySourceUrl,
  _queryWarnTop,
  _queryGoodsTop
}
